package org.kunlong.qgen.common.util;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.kunlong.qgen.boot.Boot;
import org.kunlong.qgen.config.Configuration;
import org.kunlong.qgen.domain.QuestionType;
import org.kunlong.qgen.parser.Parser;
/**
 * The class will control all the configuration loading
 * */
public final class ConfigurationManager {
	//default location of question
	private static final String DEFAULT_QUESTIONS_LOCATION = Boot.CURRENT_DIRECTORY + "/question";
	private ConfigurationManager() {
		// TODO Auto-generated constructor stub
	}
	private static Configuration configuration=new Configuration();
	
	public static void initConfiguration(File configFile) throws Exception {
		System.out.println("Default Question File location:"+DEFAULT_QUESTIONS_LOCATION);
		configuration.getQuestionDirs().add(DEFAULT_QUESTIONS_LOCATION);
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(configFile);
		Element settings = document.getRootElement();
		resolveQuestionDirs(settings);
		resolveQuestionTypes(settings);
	}
	
	/**
	 * get all question type
	 * */
	@SuppressWarnings("unchecked")
	private static void resolveQuestionTypes(Element settings){
		Element questionTypes=settings.element("QuestionTypes");
		List<Element> types=questionTypes.elements("QuestionType");
		for(Element t:types){
			String name=t.element("name").getText();
			String description=t.element("description").getText().trim();
			int sequence;
			try{
				sequence=Integer.parseInt(t.element("sequence").getText().trim());
			}catch (Exception e) {
				//any problem, just set to 0,that is the last
				System.out.println("Error Resolve sequence of "+name);
				e.printStackTrace();
				sequence=0;
			}
			String paserClassName=t.element("parser").getText().trim();
			QuestionType questionType=new QuestionType();
			questionType.setName(name);
			questionType.setDescription(description);
			questionType.setSequence(sequence);
			Parser parser=null;
			//any problem below, just continue 
			try{
				parser=(Parser)Class.forName(paserClassName).newInstance();
				parser.setQuestionType(questionType);
			}catch (ClassNotFoundException e) {
				System.out.println("Can not found Class:"+paserClassName);
				e.printStackTrace();
				continue;
			}catch (ClassCastException e) {
				System.out.println(paserClassName+" Must implement Parser Interface");
				e.printStackTrace();
				continue;
			}catch (NoClassDefFoundError e) {
				e.printStackTrace();
				continue;
			} catch (InstantiationException e) {
				e.printStackTrace();
				continue;
			} catch (IllegalAccessException e) {
				e.printStackTrace();
				continue;
			}
			questionType.setParser(parser);
			//put the questionType to map
			configuration.getQuestionType().put(name, questionType);
		}
	}
	/*
	 * 
	 * */
	@SuppressWarnings("unchecked")
	private static void resolveQuestionDirs(Element settings) {
		Element questionDirs = settings.element("QuestionDirs");
		List<Element> allDirs = questionDirs.elements();
		for (Element questionDir : allDirs) {
			String newQuestionDir=questionDir.getText().trim();
			System.out.println("Find New Question Dir:"+newQuestionDir);
			configuration.getQuestionDirs().add(newQuestionDir);
		}
	}
	public static Configuration getConfiguration() {
		return configuration;
	}

	public static void setConfiguration(Configuration configuration) {
		ConfigurationManager.configuration = configuration;
	}
}
